<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 13:07
 */

namespace Home\Controller;
use Lib\Api\GameTypeApi;
use Think\Controller;


class MinuteController extends Controller {
    public function index() {
        // $this->display();
    }

    public function getData() {
        // $game_type = $_POST['type'];
        // $map_time = $_POST['map_time'];

        $limit = 30;

        $rel_list = array();
        // 获取游戏开盘时间
        $gameTypeList = GameTypeApi::getListAll(array(), "id, open_time");
        for ($z=0; $z<count($gameTypeList); $z++) {
            $gameType = $gameTypeList[$z];
            $map_time_list = array("M1"=>1, "15M"=>15, "30M"=>30, "60M"=>60);
            // $game_data_list = array();
            while(list($k,$v) = each($map_time_list)){
                // echo $k.'=>'.$v.'<br />';
                $map_time = $v;

                $time_i = $map_time - date('i') - 1 + date('i', strtotime($gameType['open_time']));
                $d_time = date('Y-m-d H:i');
                $timeInterval = date("Y-m-d H:i", strtotime("$d_time +$time_i minute"));
                // $timeInterval = date('Y-m-d H:i', $map_time - $d_time);

                // var_dump($timeInterval);
                $limit_date = array();
                for ($i=0; $i<$limit; $i++) {
                    $d = $this->getDate(($map_time-1), $timeInterval);
                    array_push($limit_date, $d);
                }

                sort($limit_date);
                $sql_list = array();
                for ($i=0; $i<count($limit_date); $i++) {
                    $sql_temp = "select open_value, substring_index(group_concat(close_value order by create_date desc),',',1) close_value";
                    $sql_temp .= ",max(max_value) as max_value, min(min_value) as min_value";
                    // $sql_temp .= ",substring_index(group_concat(create_date order by create_date desc),',',1) time";
                    $end_time = $limit_date[$i]['end_time'];
                    $sql_temp .= ",'".date("Y-m-d H:i", strtotime("$end_time +1 minute"))."' time";
                    $sql_temp .= " from fs_minute ";
                    $sql_temp .= "where game_type =".$gameType['id']." and (DATE_FORMAT(create_date, '%Y-%m-%d %H:%i') >= '".$limit_date[$i]['begin_time']."' and DATE_FORMAT(create_date, '%Y-%m-%d %H:%i') <= '".$limit_date[$i]['end_time']."')";
                    array_push($sql_list, $sql_temp);
                }

                $sql = "select * from (".implode(" union all ", $sql_list).") t order by t.time asc limit ".$limit;
                $model = new \Think\Model();
                $list = $model->query($sql);

                $game_data_list[$k] = $list;
            }
            $game_data_list["id"] = $gameType["id"];
            array_push($rel_list, $game_data_list);
        }

        $this->ajaxReturn($rel_list);
    }

    private function getDate($map_time, &$end_time) {
        $begin_time = date("Y-m-d H:i", strtotime("$end_time -$map_time minute"));
        $d['begin_time'] = $begin_time;
        $d['end_time'] = $end_time;
        $end_time = date("Y-m-d H:i", strtotime("$begin_time -1 minute"));
        return $d;
    }

    private function getDateList($open_time, $close_time, $begin_time, $end_time, $map_time, &$date_list) {
        if (strtotime($open_time) <= strtotime($begin_time) && strtotime($close_time) >= strtotime($end_time)) {
            $d['begin_time'] = date("Y-m-d H:i", strtotime("$begin_time +1 minute"));
            $d['end_time'] = date('Y-m-d H:i', strtotime($end_time));
            array_push($date_list, $d);
            $this->getDateList($open_time, $close_time, $end_time, date("Y-m-d H:i", strtotime("$end_time +$map_time minute")),$map_time, $date_list);
        }
    }
}